﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Atlandiz.Data;

namespace Atlandiz.EnvanterVeZimmetYonetimi
{
    public partial class ListePersonel : DevExpress.XtraEditors.XtraForm
    {
        public bool ForSelection = false;

        public ListePersonel()
        {
            InitializeComponent();
        }

        public void PersonelYukle()
        {
            String Sorgu = "Select \n"+
                "p.pID,\n"+
                "p.Ad,\n"+
                "p.Soyad,\n"+
                "p.Gorev,\n"+
                "d.DepAdi \n"+
                    "from Personel p \n"+
                       "left join Departman d on p.dID=d.dID \n"+
                    "Where p.isDeleted=0";

            _lwPersonelListesi.Items.Clear();

            DataTable dt = Army.SQL().GetDataTable(Sorgu);
            foreach (DataRow item in dt.Rows)
            {
                ListViewItem ls = new ListViewItem(item[0].ToString());
                ls.SubItems.Add(item[1].ToString());
                ls.SubItems.Add(item[2].ToString());
                ls.SubItems.Add(item[3].ToString());
                ls.SubItems.Add(item[4].ToString());

                _lwPersonelListesi.Items.Add(ls);
            }

        }


        private void KullaniciLst_Load(object sender, EventArgs e)
        {
            Army.EventConfirm = false;
            PersonelYukle();
        }


        public void KDuzenle()
        {
            FormPersonel KullaniciFormu = new FormPersonel();
            KullaniciFormu.KayID = int.Parse(_lwPersonelListesi.SelectedItems[0].SubItems[0].Text);
            KullaniciFormu.ShowDialog();
            PersonelYukle();
        }

      

        private void düzenleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Army.UserTipi == 1)
            {
                if (_lwPersonelListesi.SelectedItems.Count > 0)
                {
                    KDuzenle();
                }
                else
                {
                    MessageBox.Show("Lütfen, düzenlenecek kaydı seçiniz...");
                }
            }
            else
            {
                MessageBox.Show("Bu işlem için Yönetici olmanız gerekir!", "Yetkisiz İşlem", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

        }

        private void silToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Army.UserTipi == 1)
            {
                if (_lwPersonelListesi.SelectedItems.Count > 0)
                {
                    PopupAreYouSure SilOnay = new PopupAreYouSure();
                    Army.EventMessage = "Seçilen Personel Silinecek,\nEmin misiniz?";
                    SilOnay.ShowDialog();

                    if (Army.EventConfirm)
                    {
                        Army.SQL().cmd("Update Personel SET isDeleted=1 where pID=" + _lwPersonelListesi.SelectedItems[0].SubItems[0].Text.ToString());
                        MessageBox.Show("Seçilen Personel Silindi.", "Kayıt Silindi.", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        PersonelYukle();
                        //sil komutu
                    }
                }

            }
            else
            {
                MessageBox.Show("Bu işlem için Yönetici olmanız gerekir!", "Yetkisiz İşlem", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }



        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            if (_lwPersonelListesi.SelectedItems.Count > 0 & ForSelection)
            {
                Army.SecilenID = int.Parse(_lwPersonelListesi.SelectedItems[0].SubItems[0].Text);
                Army.EventConfirm = true;
                this.Close();
            }
        }

        private void _btnYeni_Click(object sender, EventArgs e)
        {
            FormPersonel k = new FormPersonel();
            k.ShowDialog();
            PersonelYukle();
        }

        private void _btnKapat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void _btnListeGuncelle_Click(object sender, EventArgs e)
        {
            PersonelYukle();
        }

        private void _btnAra_Click(object sender, EventArgs e)
        {
            String Sorgu = "Select \n" +
                "p.pID,\n" +
                "p.Ad,\n" +
                "p.Soyad,\n" +
                "p.Gorev,\n" +
                "d.DepAdi \n" +
                    "from Personel p \n" +
                       "left join Departman d on p.dID=d.dID \n" +
                    "Where p.isDeleted=0 and \n" +
                        "p.Ad like '%" + _txtAraString.Text.ToString().Replace("'", "''") + "%' or \n" +
                        "p.Soyad like '%" + _txtAraString.Text.ToString().Replace("'", "''") + "%' or \n" +
                        "p.Gorev like '%" + _txtAraString.Text.ToString().Replace("'", "''") + "%' or \n" +
                        "d.DepAdi like '%" + _txtAraString.Text.ToString().Replace("'", "''") + "%'";

            _lwPersonelListesi.Items.Clear();

            DataTable dt = Army.SQL().GetDataTable(Sorgu);
            foreach (DataRow item in dt.Rows)
            {
                ListViewItem ls = new ListViewItem(item[0].ToString());
                ls.SubItems.Add(item[1].ToString());
                ls.SubItems.Add(item[2].ToString());
                ls.SubItems.Add(item[3].ToString());
                ls.SubItems.Add(item[4].ToString());

                _lwPersonelListesi.Items.Add(ls);
            }
            _txtAraString.Focus();
        }
    }
}